package com.triones.yjj.controller.api;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.service.ISysDictDataService;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.triones.biz.infra.service.file.FileService;
import com.triones.yjj.entity.ActivityDO;
import com.triones.yjj.entity.WithdrawalRecordDO;
import com.triones.yjj.entity.bo.*;
import com.triones.yjj.entity.vo.*;
import com.triones.yjj.service.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 小程序商家api
 *
 * @Author: triones
 */
@SaIgnore
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/wx/staff")
public class YjjMerchantApiController extends BaseController {

    private final ISysDictDataService dictDataService;

    private final WeChatManager weChatManager;

    private final ActivityService activityService;

    private final MemberService memberService;

    private final OrderService orderService;

    private final StaffService staffService;

    private final ShareService shareService;

    private final MemberBrowseService memberBrowseService;

    private final InviteRewardService inviteRewardService;

    private final MemberActivityRelateService memberActivityRelateService;

    private final MemberDistributionRecordService memberDistributionRecordService;

    private final SettledStoreService settledStoreService;

    /**
     * 员工注册申请
     *
     * @param params
     * @return
     */
    @PostMapping("/register")
    public R<T> register(@Valid WxStaffRegisterParams params) {
        return staffService.register(params);
    }

    /**
     * 员工注册并登录
     *
     * @param params
     * @return
     */
    @PostMapping("/registerAndLogin")
    public R<WxStaffVO> registerAndLogin(@Valid WxStaffRegisterParams params) {
        R<T> result = staffService.register(params);
        if (result.getCode() == 200) {
            WxStaffVO wxStaffVO = weChatManager.staffWxLogin(params.getUserId(), params.getOpenId());
            if (ObjectUtil.isNotEmpty(wxStaffVO)) {
                return R.ok("登录成功", wxStaffVO);
            } else {
                return R.fail("登录失败");
            }
        } else {
            return R.fail(result.getMsg());
        }


    }

    /**
     * 用户登录
     *
     * @param userId
     * @param openId
     * @return
     */
    @PostMapping("/login/{userId}")
    public R<WxStaffVO> login(@NotNull(message = "商家不能为空") @PathVariable Long userId, @NotBlank(message = "openId不能为空") String openId) {
        WxStaffVO wxStaffVO = weChatManager.staffWxLogin(userId, openId);
        if (ObjectUtil.isNotEmpty(wxStaffVO)) {
            return R.ok("登录成功", wxStaffVO);
        } else {
            return R.fail("登录失败");
        }
    }

    /**
     * 用户注册成功登录
     *
     * @param userId
     * @param openId
     * @return
     */
    @PostMapping("/regLogin")
    public R<WxStaffVO> regLogin(@NotNull(message = "系统id不能为空") Long userId, @NotBlank(message = "openId不能为空") String openId) {
        WxStaffVO wxStaffVO = weChatManager.staffWxLogin(userId, openId);
        if (ObjectUtil.isNotEmpty(wxStaffVO)) {
            return R.ok("登录成功", wxStaffVO);
        } else {
            return R.fail("登录失败");
        }
    }

    /**
     * 修改信息（姓名、手机号）
     *
     * @param params
     * @return
     */
    @PostMapping("/editBaseInfo")
    public R<WxStaffDetailVO> editBaseInfo(@Valid WxStaffUpdateParams params) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(params.getToken());
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            return R.fail(401, "登录失效！");
        }
        staffService.updateBaseInfo(params, wxStaffDetailVO.getId());
        wxStaffDetailVO.setRealname(params.getRealname());
        wxStaffDetailVO.setMobile(params.getMobile());
        weChatManager.reCacheStaff(params.getToken(), wxStaffDetailVO);
        return R.ok("修改成功", wxStaffDetailVO);
    }

    /**
     * 修改二维码
     *
     * @param qr    二维码
     * @param token
     * @return
     */
    @PostMapping("/editQr")
    public R<WxStaffDetailVO> editQr(@NotBlank(message = "二维码路径不能为空") String qr, @NotBlank(message = "token不能为空") String token) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(token);
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            return R.fail(401, "登录失效！");
        }
        staffService.updateQrCode(qr, wxStaffDetailVO.getId());
        wxStaffDetailVO.setWxQrCode(qr);
        weChatManager.reCacheStaff(token, wxStaffDetailVO);
        return R.ok("修改成功", wxStaffDetailVO);
    }

    /**
     * 员工分享
     *
     * @param activityId
     * @param token
     * @return
     */
    @PostMapping("/{activityId}/share")
    public R<Map<String, Object>> addShare(@NotNull(message = "活动id不能为空") @PathVariable Long activityId, @NotBlank(message = "token不能为空") String token) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(token);
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            return R.fail(401, "登录失效！");
        }

        Map<String, Object> map = new HashMap<>();
        Long shareId = shareService.saveStaffShare(activityId, wxStaffDetailVO);
        // 更新或创建客户与活动的关联
        memberActivityRelateService.createMemberActivityRelate(wxStaffDetailVO.getMemberId(), activityId);
        //更新 最新访问的活动
        memberService.uploadLatelyActivity(wxStaffDetailVO.getMemberId(), activityId);
        map.put("shareId", shareId);
        return R.ok(map);
    }

    /**
     * 查询员工的商家活动列表
     *
     * @param token
     * @return
     */
    @GetMapping("/staffActivityList")
    public R<TableDataInfo<StaffActivityListVO>> staffActivityList(@NotBlank(message = "token不能为空") String token, PageQuery pageQuery) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(token);
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            return R.fail(401, "登录失效！");
        }
        TableDataInfo<StaffActivityListVO> activityOrder = activityService.getStaffActivityPageList(wxStaffDetailVO, pageQuery);
        return R.ok(activityOrder);
    }

    /**
     * 活动详情
     *
     * @param id
     * @param token
     * @return
     */
    @GetMapping("/activity/{id}")
    public R<ActivityVO> getActivityById(@NotNull(message = "活动id不能为空") @PathVariable Long id, @NotBlank(message = "token不能为空") String token) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(token);
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            return R.fail(401, "登录失效！");
        }
        ActivityVO activityVO = activityService.getDetailById(id);
        return R.ok(activityVO);
    }

    @GetMapping("/activity/{activityId}/dataCount")
    public R<WxActivityCountVO> dataCount(@NotNull(message = "活动id不能为空") @PathVariable Long activityId, @NotBlank(message = "token不能为空") String token) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(token);
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            return R.fail(401, "登录失效！");
        }
        WxActivityCountVO activityCountVO = new WxActivityCountVO();
        //今日分享次数
        Long shareNum = shareService.getTodayShareCount(wxStaffDetailVO, activityId);
        activityCountVO.setShareNum(shareNum.intValue());
        //总分享次数
        ShareVO shareVO = shareService.getMemberShareInfo(activityId, wxStaffDetailVO.getMemberId(), wxStaffDetailVO.getUserId());
        if (ObjectUtil.isNotEmpty(shareVO)) {
            activityCountVO.setShareTotalNum(shareVO.getShareNumber());
        } else {
            activityCountVO.setShareTotalNum(0);
        }
        WxVisitParams visitParams = new WxVisitParams();
        visitParams.setStaffId(wxStaffDetailVO.getId());
        visitParams.setActivityId(activityId);
        visitParams.setUserId(wxStaffDetailVO.getUserId());
        visitParams.setTodayData(1);
        //今日访客
        Long browseNum = memberBrowseService.visitCount(visitParams);
        activityCountVO.setBrowseNum(browseNum.intValue());
        visitParams.setTodayData(2);
        //总访客
        Long browseTotalNum = memberBrowseService.visitCount(visitParams);
        activityCountVO.setBrowseTotalNum(browseTotalNum.intValue());
        //今日报名
        Long applyNum = orderService.orderCount(wxStaffDetailVO, activityId, true);
        activityCountVO.setApplyNum(applyNum.intValue());
        //总报名
        Long applyTotalNum = orderService.orderCount(wxStaffDetailVO, activityId, false);
        activityCountVO.setApplyTotalNum(applyTotalNum.intValue());
        return R.ok(activityCountVO);
    }

    /**
     * 订单-扫码核销(员工扫客户码)
     *
     * @param orderId
     * @param token
     * @return
     */
    @PostMapping("/verificationOrder")
    public R<T> verificationOrder(@NotNull(message = "订单id") Long orderId,
                                  @NotBlank(message = "token不能为空") String token) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(token);
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            return R.fail(401, "登录失效！");
        }
        return orderService.staffVerificationOrder(wxStaffDetailVO, orderId);
    }

    /**
     * 礼品-扫码核销(员工扫客户码)
     *
     * @param verificationCode
     * @param token
     * @return
     */
    @PostMapping("/verificationReward")
    public R<T> verificationReward(@NotBlank(message = "核销码不能为空") String verificationCode, @NotBlank(message = "token不能为空") String token) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(token);
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            return R.fail(401, "登录失效！");
        }
        return inviteRewardService.staffVerificationReward(wxStaffDetailVO, verificationCode);
    }

    /**
     * 最新活动详情
     *
     * @param token
     * @return
     */
    @GetMapping("/newActivity")
    public R<ActivityVO> newActivity(@NotBlank(message = "token不能为空") String token) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(token);
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            return R.fail(401, "登录失效！");
        }
        ActivityVO activityVO = activityService.getStaffNewActivity(wxStaffDetailVO.getId(), wxStaffDetailVO.getUserId());
        return R.ok(activityVO);
    }

    /**
     * 查询员工的核销记录
     *
     * @param params
     * @return
     */
    @GetMapping("/staffVerificationList")
    public R<TableDataInfo<WxVerificationOrderVO>> staffVerificationList(@Valid WxVerificationOrderParams params, PageQuery pageQuery) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(params.getToken());
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            return R.fail(401, "登录失效！");
        }
        params.setStaffId(wxStaffDetailVO.getId());
        TableDataInfo<WxVerificationOrderVO> verificationOrder = orderService.getVerificationOrderList(params, pageQuery);
        return R.ok(verificationOrder);
    }

    /**
     * 员工订单
     *
     * @param token
     * @param pageQuery
     * @return
     */
    @GetMapping("/orderList")
    public R<TableDataInfo<WxStaffOrderVO>> orderList(@NotBlank(message = "token不能为空") String token, PageQuery pageQuery) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(token);
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            return R.fail(401, "登录失效！");
        }
        TableDataInfo<WxStaffOrderVO> verificationOrder = orderService.orderList(wxStaffDetailVO, pageQuery);
        return R.ok(verificationOrder);
    }

    /**
     * 员工累计收益和余额
     *
     * @param token
     * @return
     */
    @GetMapping("/totalDistributionAmount")
    public R<Map> totalDistributionAmount(@NotBlank(message = "token不能为空") String token) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(token);
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            return R.fail(401, "登录失效！");
        }
        Map map = new HashMap();
        BigDecimal amount = BigDecimal.ZERO;
        BigDecimal walletAmount = BigDecimal.ZERO;
        MemberDetailVO memberDetailVO = memberService.selectDetailById(wxStaffDetailVO.getMemberId());
        if (ObjectUtil.isNotEmpty(memberDetailVO)) {
            amount = memberDetailVO.getDistributionAmount();
            walletAmount = memberDetailVO.getWalletAmount();
        }
        map.put("totalAmount", amount);//累计收益
        //余额
        map.put("walletAmount", walletAmount);
        return R.ok("成功", map);
    }

    /**
     * 查询员工佣金明细-佣金收益
     *
     * @param token
     * @return
     */
    @GetMapping("/getDistributionRecordList")
    public R<TableDataInfo<WxMemberDistributionRecordVO>> getDistributionRecordList(@NotBlank(message = "token不能为空") String token, PageQuery pageQuery) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(token);
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            return R.fail(401, "登录失效！");
        }
        TableDataInfo<WxMemberDistributionRecordVO> verificationOrder = memberDistributionRecordService.getList(wxStaffDetailVO.getMemberId(), pageQuery);
        return R.ok(verificationOrder);
    }


    @GetMapping("/getOrderInfo/{orderId}")
    public R<ActivityOrderUserVO> getOrderInfo(@NotNull(message = "订单id不能为空") @PathVariable Long orderId, @NotBlank(message = "token不能为空") String token) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(token);
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            return R.fail(401, "登录失效！");
        }
        ActivityOrderUserVO info = orderService.getActivityOrderDetail(orderId);
        return R.ok(info);
    }

    @PostMapping("/applyWithdraw")
    public R<T> applyWithdraw(@NotNull(message = "提现金额不能为空") Double amount, @NotBlank(message = "token不能为空") String token) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(token);
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            return R.fail(401, "登录失效！");
        }
        memberService.applyWithdraw(wxStaffDetailVO.getMemberId(), amount,"STAFF");
        return R.ok("提现申请成功！");
    }

    /**
     * 礼品详情
     * @param verificationCode
     * @param token
     * @return
     */
    @GetMapping("/getInviteRewardInfo")
    public R<WxMemberInviteRewardVO> getInviteRewardInfo(@NotNull(message = "礼品核销码不能为空") String verificationCode, @NotBlank(message = "token不能为空") String token) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(token);
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            return R.fail(401, "登录失效！");
        }
        WxMemberInviteRewardVO info = inviteRewardService.getMemberRewardInfoByCode(verificationCode);
        return R.ok(info);
    }

    /**
     * 访客列表
     * @param params
     * @param pageQuery
     * @return
     */
    @GetMapping("/getVisitList")
    public R<TableDataInfo<WxStaffMemberBrowseVO>> getVisitList(WxVisitSearchParams params, PageQuery pageQuery) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(params.getToken());
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            return R.fail(401, "登录失效！");
        }
        params.setUserId(wxStaffDetailVO.getUserId());
        params.setStaffId(wxStaffDetailVO.getId());
        TableDataInfo<WxStaffMemberBrowseVO> visitList = memberBrowseService.getVisitList(params,pageQuery);
        return R.ok(visitList);
    }

    /**
     * 报名列表
     * @param params
     * @param pageQuery
     * @return
     */
    @GetMapping("/getStaffMemberOrderList")
    public R<TableDataInfo<WxStaffMemberOrderVO>> getStaffMemberOrderList(WxStaffMemberOrderParams params, PageQuery pageQuery) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(params.getToken());
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            return R.fail(401, "登录失效！");
        }
        params.setUserId(wxStaffDetailVO.getUserId());
        params.setStaffId(wxStaffDetailVO.getId());
        TableDataInfo<WxStaffMemberOrderVO> visitList = orderService.getStaffMemberOrderList(params,pageQuery);
        return R.ok(visitList);
    }

    @GetMapping("/{activityId}/getOneDistributionRecordList")
    public R<TableDataInfo<WxMemberDistributionRecordVO>> getOneDistributionRecordList(@NotNull(message = "活动id不能为空") @PathVariable Long activityId,@NotBlank(message = "token不能为空") String token, PageQuery pageQuery) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(token);
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            return R.fail(401, "登录失效！");
        }
        TableDataInfo<WxMemberDistributionRecordVO> verificationOrder = memberDistributionRecordService.getOneDistributionRecordList(wxStaffDetailVO.getMemberId(),activityId, pageQuery);
        return R.ok(verificationOrder);
    }

    @GetMapping("/{activityId}/getTwoDistributionRecordList")
    public R<TableDataInfo<WxMemberDistributionRecordVO>> getTwoDistributionRecordList(@NotNull(message = "活动id不能为空") @PathVariable Long activityId,@NotBlank(message = "token不能为空") String token, PageQuery pageQuery) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(token);
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            return R.fail(401, "登录失效！");
        }
        TableDataInfo<WxMemberDistributionRecordVO> verificationOrder = memberDistributionRecordService.getTwoDistributionRecordList(wxStaffDetailVO.getMemberId(),activityId, pageQuery);
        return R.ok(verificationOrder);
    }

    @GetMapping("/{activityId}/getOneDistributionCount")
    public R<Map<String,Object>> getOneDistributionCount(@NotNull(message = "活动id不能为空") @PathVariable Long activityId,@NotBlank(message = "token不能为空") String token) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(token);
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            return R.fail(401, "登录失效！");
        }
        Map<String,Object> map = memberDistributionRecordService.getDistributionCount(wxStaffDetailVO.getMemberId(),activityId, 1);
        return R.ok(map);
    }

    @GetMapping("/{activityId}/getTwoDistributionCount")
    public R<Map<String,Object>> getTwoDistributionCount(@NotNull(message = "活动id不能为空") @PathVariable Long activityId,@NotBlank(message = "token不能为空") String token) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(token);
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            return R.fail(401, "登录失效！");
        }
        Map<String,Object> map = memberDistributionRecordService.getDistributionCount(wxStaffDetailVO.getMemberId(),activityId, 2);
        return R.ok(map);
    }

    /**
     * 查询员工商家列表
     * @param openId
     * @return
     */
    @GetMapping("/staffUserList")
    public R<List<WxStaffSysUseVO>> staffUserList(@NotBlank(message = "openId不能为空") String openId) {
        List<WxStaffSysUseVO> list =  staffService.getStaffSysUserList(openId);
        return R.ok(list);
    }

    //通知设置（客户报名提醒、活动开始前5分钟提醒、活动结束前5分钟提醒）

    //我的钱包-、下单奖励

    //我的钱包-余额

    /**
     * 员工获取客户的分享id和分享记录id
     * @param activityId
     * @param token
     * @return
     */
    @GetMapping("/getShareIdAndShareRecordId/{activityId}/{memberId}")
    @RepeatSubmit(interval = 2, timeUnit = TimeUnit.SECONDS, message = "{repeat.submit.message}")
    public R<Map<String,Object>> getShareIdAndShareRecordId(@NotNull(message = "活动id不能为空") @PathVariable Long activityId,
                                                            @NotNull(message = "客户id不能为空") @PathVariable Long memberId,
                                                            @NotBlank(message = "token不能为空") String token) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(token);
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            return R.fail(401, "登录失效！");
        }
        Map<String,Object> map = staffService.getShareIdAndShareRecordId(wxStaffDetailVO,activityId,memberId);
        return R.ok(map);
    }

    /**
     * 查询员工商家列表
     * @param id
     * @return
     */
    @GetMapping("/staffInfo/{id}")
    public R<StaffVO> getStaffInfo(@NotNull(message = "员工id不能为空") @PathVariable("id") String id) {
        return R.ok(staffService.getStaffInfo(id));
    }

    /**
     * 员工查询 特约商户提现申请
     */
    @GetMapping("/paymentAudit/pageList")
    public R<TableDataInfo<WithdrawalRecordCustomVO>> staffGetPaymentAudit(@NotBlank(message = "token不能为空") String token, WithdrawalListParams params ,PageQuery pageQuery) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(token);
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            return R.fail(401, "登录失效！");
        }
        if (Convert.toInt(wxStaffDetailVO.getPaymentAudit(),0) == 1) {
            return R.ok(settledStoreService.withdrawalPageList(params, pageQuery));
        }
        return R.ok();
    }

    /**
     * 查询系统用户列表
     */
    @GetMapping("/sysUserList")
    public R<List<SysUser>> sysUserList(@NotBlank(message = "token不能为空") String token) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(token);
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            return R.fail(401, "登录失效！");
        }
        if (Convert.toInt(wxStaffDetailVO.getPaymentAudit(),0) == 1) {
            return R.ok(settledStoreService.sysUserList());
        }
        return R.ok();
    }

    /**
     * 完成打款
     */
    @PostMapping("/done")
    public R<T> doneWithdrawal (@NotBlank(message = "token不能为空") String token,@RequestBody WithdrawalRecordBO bo) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(token);
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            return R.fail(401, "登录失效！");
        }
        if (Convert.toInt(wxStaffDetailVO.getPaymentAudit(),0) != 1) {
            throw new ServiceException("有没有审核权限");
        }
        settledStoreService.doneWithdrawal(bo);
        return R.ok("操作成功");
    }

    /**
     * 提现状态列表
     */
    @GetMapping("/withdrawalStatusList")
    public R<List<SysDictData>> getWithdrawalStatusList() {
        SysDictData dictData = new SysDictData();
        dictData.setDictType("withdrawal_status");
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return R.ok(list);
    }

    /**
     * 查询活动申请清算和已同意申请清算列表（分页）
     */
    @GetMapping("/settlement/apply/pageList")
    public TableDataInfo<ApplySettlementVO> applySettlementPageList(
        @NotBlank(message = "token不能为空") String token,
        ActivityDO dto,
        String keyword,
        PageQuery pageQuery) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(token);
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            throw new ServiceException("登录失效!",  401);
        }
        if (Convert.toInt(wxStaffDetailVO.getPaymentAudit(),0) != 1) {
            throw new ServiceException("有没有审核权限");
        }
        return activityService.applySettlementPageList(keyword,dto, pageQuery);
    }

    /**
     * 清算
     */
    @GetMapping("/settlement/{ids}")
    public R<Void> settlement(@NotBlank(message = "token不能为空") String token,@PathVariable Long[] ids) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(token);
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            throw new ServiceException("登录失效!",  401);
        }
        if (Convert.toInt(wxStaffDetailVO.getPaymentAudit(),0) != 1) {
            throw new ServiceException("有没有审核权限");
        }
        return toAjax(activityService.settlement(ids));
    }

    /**
     * 将自己从这个商家员工中移除
     */
    @PostMapping("/removeStaff")
    public R<WxStaffDetailVO> removeStaff(@NotNull(message = "商家id不能为空") Long uid, @NotBlank(message = "token不能为空") String token) {
        WxStaffDetailVO wxStaffDetailVO = weChatManager.getCacheStaff(token);
        if (ObjectUtil.isEmpty(wxStaffDetailVO)) {
            return R.fail(401, "登录失效！");
        }
        staffService.removeStaff(uid,wxStaffDetailVO.getWxUserId());
        return R.ok("操作成功");
    }
}
